Thus this chapter briefly introduces three different packages for building such visualizations. Instead of offering an in-depth description (as with ggplot2), this chapter provides a high-level “tour” of these packages. The first two (Plotly and Bokeh) are able to add basic interactions to the plots you might make with ggplot2, while the third (Leaflet) is used to create interactive map visualizations. Picking among these (and other) packages depends on the type of interactions you want your visualization to provide, the ease of use, the clarity of the package documentation, and your aesthetic preferences. And because these open source projects are constantly evolving, you will need to reference their documentation to make the best use of these packages. Indeed, exploring these packages further is great practice in learning to use new R packages!
The first two sections demonstrate creating interactive plots of the iris data set, a canonical data set in the machine learning and visualization world in which a flower’s species is predicted using features of that flower. The data set is built into the R software, and is partially shown in Figure 17.1.
For example, you can use ggplot2 to create a static visualization of flower species in terms of the length of the petals and the sepals (the container for the buds), as shown in Figure 17.2:
# Create a static plot of the iris data set
ggplot(data = iris) + # iris is a data set in Standard R
geom_point(mapping = aes(x = Sepal.Width, y = Petal.Width, color = Species))
The following sections show how to use the plotly and rbokeh packages to make this plot interactive. The third section of the chapter then explores interactive mapping with the leaflet package.
Plotly is a piece of visualization software that provides open source APIs (programming libraries) for creating interactive visualizations in a wide variety of languages, including R, Python, Matlab, and JavaScript. By default, Plotly charts support a wide range of user interactions, including tooltips on hover, panning, and zooming in on selected regions.
Plotly: https://plot.ly/r/
Plotly is an external package (like dplyr or ggplot2), so you will need to install and load the package before you can use it. This will make all of the plotting functions you will need available.
With the package loaded, there are two main ways to create interactive plots. - First, you can take any plot created using ggplot2 and “wrap” it in a Plotly plot, thereby adding interactions to it. - You do this by taking the plot returned by the ggplot() function and passing it into the ggplotly() function provided by the plotly package:
Plotly ggplot2 library: https://plot.ly/ggplot2/ (be sure to check the navigation links in the menu on the left).
# Create (and store) a scatterplot of the `iris` data set using ggplot2
flower_plot <- ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Width, y = Petal.Width, color = Species))
# Make the plot interactive by passing it to Plotly's `ggplotly()` function
ggplotly(flower_plot)
This will render an interactive version of the iris plot! You can hover the mouse over any geometry element to see details about that data point, or you can click and drag in the plot area to zoom in on a cluster of points (see Figure 17.3).
In addition to making ggplot plots interactive, you can use the Plotly API itself (e.g., calling its own functions) to build interactive graphics. For example, the following code will create an equivalent plot of the iris data set:
Plotly plots are created using the plot_ly() function, which is a sort of corollary to the ggplot() function. The plot_ly() function takes as arguments details about how the chart should be rendered. For example, in the preceding code, arguments are used to specify the data, the aesthetic mappings, and the plot type (that is, geometry). Aesthetic mappings are specified as formulas (using a tilde ~), indicating that the visual channel is a “function of” the data column. Also note that Plotly will try to “guess” values such as type and mode if they are left unspecified (and in which case it will print out a warning in the console).
# Create an interactive plot of the iris data set using Plotly
plot_ly(
data = iris, # pass in the data to be visualized
x = ~Sepal.Width, # use a formula to specify the column for the x-axis
y = ~Petal.Width, # use a formula to specify the column for the y-axis
color = ~Species, # use a formula to specify the color encoding
type = "scatter", # specify the type of plot to create
mode = "markers" # determine the "drawing mode" for the scatter (points)
)
For a complete list of options available to the plot_ly() function, see the official documentation. It’s often easiest to learn to make Plotly charts by working from one of the many examples. We suggest that you find an example that is close to what you want to produce, and then read that code and modify it to fit your particular use case.
Plotly: Basic Charts example gallery: https://plot.ly/r/#basic-charts
In addition to using the plot_ly() function to specify how the data will be rendered, you can add other chart options, such as titles and axes labels. These are specified using the layout() function, which is conceptually similar to the labs() and theme() functions from ggplot2. Plotly’s layout() function takes as an argument a Plotly chart (e.g., one returned by the plot_ly() function), and then modifies that object to produce a chart with a different layout. Most commonly, this is done by piping the Plotly chart into the layout() function:
# Create a plot, then pipe that plot into the `layout()` function to modify it
# (Example adapted from the Plotly documentation)
plot_ly(
data = iris, # pass in the data to be visualized
x = ~Sepal.Width, # use a formula to specify the column for the x-axis
y = ~Petal.Width, # use a formula to specify the column for the y-axis
color = ~Species, # use a formula to specify the color encoding
type = "scatter", # specify the type of plot to create
mode = "markers" # determine the "drawing mode" for the scatter (points)
) %>%
layout(
title = "Iris Data Set Visualization", # plot title
xaxis = list(title = "Sepal Width", ticksuffix = "cm"), # axis label + format
yaxis = list(title = "Petal Width", ticksuffix = "cm") # axis label + format
)
The chart created by this code is shown in Figure 17.4. The xaxis and yaxis arguments expect lists of axis properties, allowing you to control many aspects of each axis (such as the title and the ticksuffix to put after each numeric value in the axis). You can read about the structure and options to the other arguments in the API documentation.